package org.viettan.android.main.activities.common;

import org.viettan.android.main.R;
import org.viettan.android.main.model.Article;
import org.viettan.android.main.view.ViewUtility;

import roboguice.inject.InjectView;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;

import com.google.inject.Inject;

/**
 * Displays a an article inside a scrollview.
 */
public abstract class BaseArticleActivity extends VietTanBaseActivity {
    @Inject
    private ViewUtility viewUtility;

    @InjectView(R.id.image)
    ImageView imageView;

    @InjectView(R.id.body)
    WebView textView;

    protected Article article;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.article);
    }

    protected void displayArticle(Article article) {
	this.article = article;
	viewUtility.setText(this, R.id.title, article.getTitle());
	Uri articleImageUrl = article.getImageLargeUrl();
	if (article.getImageResourceId() != 0) {
	    int resourceId = article.getImageResourceId();
	    imageView.setVisibility(View.VISIBLE);
	    imageView.setImageResource(resourceId);
	} else if (articleImageUrl != null) {
	    viewUtility.setImage(this, R.id.image, true, articleImageUrl);
	} else {
	    imageView.setVisibility(View.GONE);
	}

	// transparent background for the web view
	textView.setBackgroundColor(0x00000000);

	// viewUtility.setText(this, R.id.body, article.getBody());
	viewUtility.setHtml(this, R.id.body, article.getBody());
    }
}
